/**
 * @program: 2.19 Map
 * @description:
 * @author: 李鑫月
 * @create: 2022-02-19 16:38
 **/
class Node{
    public int val;
    public Node left;
    public Node right;
    public Node(int val){
        this.val=val;
    }


}
public class BinarySerachTree {
    public  Node root=null;
    public Node search(int val){
        Node cur=root;
        while (cur!=null){
            if(cur.val==val){
                return  cur;
            }
            if(cur.val<val){
                cur=cur.right;
            }else {
                cur=cur.left;
            }
        }
        return null;
    }
    public boolean insert(int val){
        if(root==null){
            root=new Node(val);
            return true;
        }
        Node cur=root;
        Node parrent=null;
        while (cur!=null){
            if(cur.val==val){
               return  false;
            }
            if(cur.val<val){
                parrent=cur;
                cur=cur.right;
            }else {
                parrent=cur;
                cur=cur.left;
            }
        }
        if(parrent.val>val){
            parrent.left=new Node(val);
        }else {
            parrent.right=new Node(val);
        }
        return true;
    }
    public boolean delete(int val) {
    return false;
    }
    public void remove(int key) {
        Node cur = root;
        Node parent = null;
        while (cur != null) {
            if(cur.val == key) {
                //这里开始删除
                removeNode(cur,parent);
                break;
            }else if(cur.val < key) {
                parent = cur;
                cur = cur.right;
            }else {
                parent = cur;
                cur = cur.left;
            }
        }
    }

    public void removeNode(Node cur,Node parent) {
        if(cur.left == null) {
            if(cur == root) {
                root = cur.right;
            }else if(cur == parent.left) {
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if(cur.right == null) {
            if(cur == root) {
                root = cur.left;
            }else if(cur == parent.left) {
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            Node ta=cur.right;
            Node tap=cur;
            while (ta.left!=null){
                tap=ta;
                ta=ta.left;
            }
                cur.val=ta.val;
            if(ta==tap.left){
                tap.left=ta.right;
            }else {
                tap.right=ta.right;
            }



        }
    }
    public void inorder(Node root){
        if(root==null) return;
        inorder(root.left);
        System.out.print(root.val+" ");
        inorder(root.right);
    }

    public static void main(String[] args) {
        int[] arr={10,23,45,1,2,67,88};
        BinarySerachTree binarySerachTree=new BinarySerachTree();
        for (int i = 0; i <arr.length ; i++) {
            binarySerachTree.insert(arr[i]);
        }
        binarySerachTree.inorder(binarySerachTree.root);
    }


}
